package leecode

func levelOrder(root *TreeNode) (result [][]int) {
	//先检查根节点
	if root == nil {
		return
	}
	dqueue := []*TreeNode{root}
	for len(dqueue) != 0 {
		temp := make([]int, 0)
		size := len(dqueue)
		for i := 0; i < size; i++ {
			temp = append(temp, dqueue[i].Val)
			//将当前节点的左节点和右节点加入队列
			if dqueue[i].Left != nil {
				dqueue = append(dqueue, dqueue[i].Left)
			}
			if dqueue[i].Right != nil {
				dqueue = append(dqueue, dqueue[i].Right)
			}
		}
		//弹出队列中的父节点
		dqueue = dqueue[size:]
		result = append(result, temp)
	}
	return
}
